package com.chengxiaoxiao.libarypartner.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.chengxiaoxiao.libarypartner.R;
import com.chengxiaoxiao.libarypartner.activity.BookActivity;
import com.chengxiaoxiao.libarypartner.activity.CategoryActivity;
import com.chengxiaoxiao.libarypartner.activity.ReturnsActivity;
import com.chengxiaoxiao.libarypartner.activity.ScanActivity;
import com.chengxiaoxiao.libarypartner.activity.SearchActivity;
import com.chengxiaoxiao.libarypartner.constants.AppConsrants;
import com.chengxiaoxiao.libarypartner.domain.Author;
import com.chengxiaoxiao.libarypartner.domain.BookImageAddress;
import com.chengxiaoxiao.libarypartner.domain.Books;
import com.chengxiaoxiao.libarypartner.domain.Category;
import com.chengxiaoxiao.libarypartner.utils.ApiAddress;
import com.chengxiaoxiao.libarypartner.utils.ToastUtils;
import com.chengxiaoxiao.libarypartner.utils.UserUtils;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.image.SmartImageView;
import com.zxing.activity.CaptureActivity;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;
import static com.chengxiaoxiao.libarypartner.R.id.editSearch;


public class IndexFragment extends Fragment
{

    /**
     * 幻灯片
     */
    private int[] listImages = new int[]{R.drawable.a, R.drawable.b, R.drawable.c};
    //List<Appppt> listPPT = null;

    private ViewPager viewPage;
    private LinearLayout lineDot;
    private int mPreviousPos;
    private EditText editFind;
    private ImageView imageScan;


    // 接收消息
    private Handler handler = new Handler()
    {
        public void handleMessage(android.os.Message msg)
        {

            int item = viewPage.getCurrentItem();
            viewPage.setCurrentItem(++item);
            handler.sendEmptyMessageDelayed(0, 3000);

        }

        ;

    };


    /**
     * 榜单
     */
    private GridView gridHome;
    private String[] homeNames = new String[]{"图书榜", "新书榜", "新书上架", "热门作者", "猜你喜欢"};
    private int[] homeIcon = {R.drawable.fragment_index_book1, R.drawable.fragment_index_book2, R.drawable.fragment_index_book3, R.drawable.fragment_index_book4, R.drawable.fragment_index_book5};


    /**
     * 分类
     */
    private GridView gridCategory;
    //private String[] categoryNames = new String[]{"历史", "文化", "物理", "儿童", "少年", "历史", "文化", "物理", "儿童", "少年"};

    private String[] categoryNames;
    private List<Category> listCategory;
    /**
     * 热门书籍
     */

    private List<Books> listBooks;

    private GridView GridHot;
    //    private String[] hotNames = new String[]{"新书", "新书榜", "新书上架", "热门作者", "猜你喜欢", "新书", "新书榜", "新书上架", "热门作者"};
    //    private int[] hotIcon = {R.drawable.fragment_index_book1, R.drawable.fragment_index_book2, R.drawable.fragment_index_book3, R.drawable.fragment_index_book4, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5};

    /**
     * 热门作者
     */
    private GridView GridHotAuthor;
    private List<Author> listAuthor;
    //    private String[] hotAuthorNames = new String[]{"新书", "新书榜", "新书上架", "热门作者", "猜你喜欢", "新书", "新书榜", "新书上架", "热门作者"};
    //    private int[] hotAuthorIcon = {R.drawable.fragment_index_book1, R.drawable.fragment_index_book2, R.drawable.fragment_index_book3, R.drawable.fragment_index_book4, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5};


    private GridView GridGuess;
    private String[] guessNames = new String[]{"新书", "新书榜", "新书上架", "热门作者", "猜你喜欢", "新书", "新书榜", "新书上架", "热门作者"};
    private int[] guessIcon = {R.drawable.fragment_index_book1, R.drawable.fragment_index_book2, R.drawable.fragment_index_book3, R.drawable.fragment_index_book4, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5, R.drawable.fragment_index_book5};


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_index, container, false);

        /**
         * 初始化布局
         */
        initView(view);


        /**
         * PPT逻辑
         */
        loadPPT();



        /**
         * 加载推荐书籍
         */
        initRecommend();




        return view;

    }

    List<BookImageAddress> listSimple = new ArrayList<>();

    /**
     * 加载推荐信息
     */
    private void initRecommend()
    {
        final AsyncHttpClient client = new AsyncHttpClient();
        client.get(ApiAddress.getApiDomain() + "/client/ClientServlet?op=recommend&uid=" + UserUtils.getNumber(getActivity()), new AsyncHttpResponseHandler()
        {
            @Override
            public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
            {

                String body = new String(responseBody);
                try
                {
                    JSONArray jsonArray = new JSONArray(body);


                    for (int i = 0; i < jsonArray.length(); i++)
                    {

                        JSONObject jsonObj = jsonArray.getJSONObject(i);

                        String address = jsonObj.getString("address");
                        String bookName = jsonObj.getString("bookName");
                        String imageSrc = jsonObj.getString("imageSrc");
                        int id = jsonObj.getInt("id");

                        BookImageAddress bia = new BookImageAddress(id, bookName, imageSrc, address);
                        listSimple.add(bia);
                    }
                    GridGuess.setAdapter(new GuessAdapter());


                } catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
            {
                //LoadDialogUtils.hidden();
                ToastUtils.makeText(getActivity(), "获取新版本失败");
            }
        });
    }


    /**
     * 初始化布局
     */
    private void initView(View view)
    {
        editFind = (EditText) view.findViewById(editSearch);

        editFind.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });



        Drawable drawable = getResources().getDrawable(R.drawable.find);
        //第一个0是距左边距离，第二个0是距上边距离，40分别是长宽
        drawable.setBounds(0, 0, 70, 70);
        //只放左边
        editFind.setCompoundDrawables(drawable, null, null, null);

        imageScan = (ImageView) view.findViewById(R.id.imageScan);
        imageScan.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(getActivity(), CaptureActivity.class);
                startActivityForResult(intent, 0);

            }
        });


        viewPage = (ViewPager) view.findViewById(R.id.viewPage);
        lineDot = (LinearLayout) view.findViewById(R.id.lineDot);
        gridHome = (GridView) view.findViewById(R.id.GridHome);
        gridCategory = (GridView) view.findViewById(R.id.GridCategory);
        GridHot = (GridView) view.findViewById(R.id.GridHot);
        GridHotAuthor = (GridView) view.findViewById(R.id.GridHotAuthor);
        GridGuess = (GridView) view.findViewById(R.id.GridGuess);

        //获取类别信息
        loadCatrgory();

        //获取热门书籍
        loadHotBooks();

        loadHotAuthor();


        gridHome.setAdapter(new MyListAdapter());


        GridGuess.setAdapter(new GuessAdapter());


    }

    /**
     * 获取热门作者信息
     */
    private void loadHotAuthor()
    {
        final AsyncHttpClient client = new AsyncHttpClient();
        client.get(ApiAddress.getApiDomain() + "/client/ClientServlet?op=hotAuthor", new AsyncHttpResponseHandler()
        {
            @Override
            public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
            {

                String body = new String(responseBody);
                try
                {
                    JSONArray jsonArray = new JSONArray(body);

                    listAuthor = new ArrayList<Author>();

                    for (int i = 0; i < jsonArray.length(); i++)
                    {

                        JSONObject jsonObj = jsonArray.getJSONObject(i);

                        String name = jsonObj.getString("name");
                        String src = jsonObj.getString("src");
                        Author author = new Author(name, src);

                        listAuthor.add(author);
                    }

                    GridHotAuthor.setAdapter(new HotAuthorAdapter());


                } catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
            {
                //LoadDialogUtils.hidden();
                ToastUtils.makeText(getActivity(), "获取新版本失败");
            }
        });
    }

    /**
     * 加载热门书籍信息
     */
    private void loadHotBooks()
    {
        final AsyncHttpClient client = new AsyncHttpClient();
        client.get(ApiAddress.getApiDomain() + "/client/ClientServlet?op=hotBook", new AsyncHttpResponseHandler()
        {
            @Override
            public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
            {

                String body = new String(responseBody);
                try
                {
                    JSONArray jsonArray = new JSONArray(body);

                    listBooks = new ArrayList<Books>();

                    for (int i = 0; i < jsonArray.length(); i++)
                    {

                        JSONObject jsonObj = jsonArray.getJSONObject(i);


                        long id = jsonObj.getInt("id");
                        String bookName = jsonObj.getString("bookName");
                        String author = jsonObj.getString("author");
                        String categoryName = jsonObj.getString("categoryName");
                        String imagesPath = jsonObj.getString("imagesPath");
                        String publisherName = jsonObj.getString("publisherName");
                        long number = jsonObj.getInt("number");

                        String address = jsonObj.getString("bookName");
                        Books book = new Books(id, bookName, author, categoryName, imagesPath, number, address, publisherName);
                        listBooks.add(book);
                    }

                    GridHot.setAdapter(new MyHotAdapter());


                } catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
            {
                //LoadDialogUtils.hidden();
                ToastUtils.makeText(getActivity(), "获取新版本失败");
            }
        });

    }

    /**
     * 加载类别信息
     */
    private void loadCatrgory()
    {

        final AsyncHttpClient client = new AsyncHttpClient();
        client.get(ApiAddress.getApiDomain() + "/client/ClientServlet?op=getCatrgory", new AsyncHttpResponseHandler()
        {
            @Override
            public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
            {

                String body = new String(responseBody);
                try
                {
                    JSONArray jsonArray = new JSONArray(body);

                    listCategory = new ArrayList<Category>();

                    for (int i = 0; i < jsonArray.length(); i++)
                    {

                        JSONObject jsonObj = jsonArray.getJSONObject(i);

                        int id = jsonObj.getInt("id");
                        String name = jsonObj.getString("name");
                        String descript = jsonObj.getString("descript");

                        Category category = new Category(id, name, descript);
                        listCategory.add(category);
                    }

                    gridCategory.setAdapter(new MyCategoryAdapter());


                } catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
            {
                //LoadDialogUtils.hidden();
                ToastUtils.makeText(getActivity(), "获取新版本失败");
            }
        });
    }

    /**
     * 接收二维码的结果
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK)
        {
            /**
             * 获取扫描的条形码
             */
            String msg = data.getExtras().getString("result");

            //判断扫描的是二维码还是条形码

            if (msg.contains("{"))
            {
                //提示非法
                //ToastUtils.makeText(getActivity(), "请扫描书籍背面ISBN码" + msg);

                try
                {
                    JSONObject json = new JSONObject(msg);

                    String op = json.getString("op");
                    String date = json.getString("date");
                    String adminId = json.getString("adminId");

                    if ("borrow".equalsIgnoreCase(op))
                    {
                        ToastUtils.makeText(getActivity(), "如需借书，请扫描书籍背面ISBN");

                    } else if ("return".equalsIgnoreCase(op))
                    {
                        returnsBook();
                    }


                } catch (Exception e)
                {

                }


            } else
            {

                Intent intent = new Intent(getActivity(), ScanActivity.class);
                intent.putExtra(AppConsrants.SCAN_MSG_BOOK_CODE, msg);
                startActivity(intent);

                ToastUtils.makeText(getActivity(), "一维码：" + msg);
            }
        }
    }

    private void returnsBook()
    {
        startActivity(new Intent(getActivity(), ReturnsActivity.class));

    }

    /**
     * 幻灯片设计
     */
    private void ppt()
    {
        // 添加小圆点
        AddDot();

        viewPage.setAdapter(new MyPageAdapter());

        // 延时两秒显示
        handler.sendEmptyMessageDelayed(0, 3000);

        // 设置事件
        viewPage.setOnPageChangeListener(new OnPageChangeListener()
        {

            @Override
            public void onPageSelected(int position)
            {
                int pos = position % listImages.length;

                // 更新小圆点
                lineDot.getChildAt(pos).setEnabled(true);// 将选中的页面的圆点设置为红色
                // 将上一个圆点变为灰色
                lineDot.getChildAt(mPreviousPos).setEnabled(false);
                // 更新上一个页面位置
                mPreviousPos = pos;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2)
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0)
            {
                // TODO Auto-generated method stub

            }
        });

        // 触摸时停止，手抬起继续
        viewPage.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View arg0, MotionEvent event)
            {

                switch (event.getAction())
                {

                    case MotionEvent.ACTION_UP:
                        handler.sendEmptyMessageDelayed(0, 3000);
                        break;
                    case MotionEvent.ACTION_DOWN:

                        handler.removeCallbacksAndMessages(null);
                        break;
                }
                return false;
            }
        });


        gridHome.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                Intent intent;
                switch (position)
                {
                    case 0:
                        break;
                    case 1:
                        break;
                }

            }
        });
    }

    /**
     * 加载幻灯片数据
     */
    private void loadPPT()
    {
        ppt();
//        final AsyncHttpClient client = new AsyncHttpClient();
//        client.get(ApiAddress.getApiDomain() + "/client/ClientServlet?op=ppt", new AsyncHttpResponseHandler()
//        {
//            @Override
//            public void onSuccess(int statusCode, org.apache.http.Header[] headers, byte[] responseBody)
//            {
//
//                String body = new String(responseBody);
//                try
//                {
//                    JSONArray jsonArray = new JSONArray(body);
//
//                    listPPT = new ArrayList<Appppt>();
//                    for (int i = 0; i < jsonArray.length(); i++)
//                    {
//
//                        JSONObject jsonObj = jsonArray.getJSONObject(i);
//
//                        int id = jsonObj.getInt("id");
//                        String picture = jsonObj.getString("picture");
//                        String title = jsonObj.getString("title");
//
//                        Appppt appppt = new Appppt(id, title, picture);
//                        listPPT.add(appppt);
//                    }
//
//                    //System.out.printf("----------------------------" + listPPT.size());
//
//                    ppt();
//
//
//                } catch (JSONException e)
//                {
//                    // TODO Auto-generated catch block
//                    e.printStackTrace();
//                }
//
//
//            }
//
//            @Override
//            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
//            {
//                //LoadDialogUtils.hidden();
//                ToastUtils.makeText(getActivity(), "获取新版本失败");
//            }
//        });

    }


    // 初始化小圆点
    private void AddDot()
    {
        for (int i = 0; i < listImages.length; i++)
        {

            ImageView image = new ImageView(getActivity());
            image.setImageResource(R.drawable.shape_point_selector);
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            params.leftMargin = 8;
            params.bottomMargin = 10;

            if (i != 0)
            {
                image.setEnabled(false);
            }
            image.setLayoutParams(params);
            lineDot.addView(image);

        }
    }

    // 幻灯片的Adapter
    class MyPageAdapter extends PagerAdapter
    {

        @Override
        public int getCount()
        {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View v, Object o)
        {
            return v == o;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            int pos = position % listImages.length;

//            SmartImageView images = new SmartImageView(getActivity());
//
//            Bitmap bitmap = new WebImage(ApiAddress.getDomain() + listPPT.get(pos).getPicture()).getBitmap(getActivity());
//
//            images.setBackground(new BitmapDrawable(bitmap));

            ImageView images = new ImageView(getActivity());
            images.setBackgroundResource(listImages[pos]);

            container.addView(images);

            return images;

        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object)
        {

            container.removeView((View) object);

        }

    }


    // 榜单的Adapter
    class MyListAdapter extends BaseAdapter
    {

        @Override
        public int getCount()
        {
            // TODO Auto-generated method stub
            return homeNames.length;
        }

        @Override
        public Object getItem(int pos)
        {
            return pos;
        }

        @Override
        public long getItemId(int id)
        {
            // TODO Auto-generated method stub
            return id;
        }

        @Override
        public View getView(final int pos, View arg1, ViewGroup arg2)
        {
            View view = View.inflate(getActivity(), R.layout.list_item_home, null);

            TextView textName = (TextView) view.findViewById(R.id.textName);
            ImageView image = (ImageView) view.findViewById(R.id.ivIcon);

            textName.setText(homeNames[pos]);

            Drawable drawable = getResources().getDrawable(homeIcon[pos]);
            drawable.mutate().setAlpha(255);

            image.setImageDrawable(drawable);

            image.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Intent intent = new Intent();

                    switch (pos)
                    {
                        case 0:
                            intent.setClass(getActivity(), CategoryActivity.class);
                            intent.putExtra("method", "bookRank");
                            startActivity(intent);
                            break;
                        case 1:
                            intent.setClass(getActivity(), CategoryActivity.class);
                            intent.putExtra("method", "newbook");
                            startActivity(intent);
                            break;
                        case 2:
                            intent.setClass(getActivity(), CategoryActivity.class);
                            intent.putExtra("method", "newbook");
                            startActivity(intent);
                            break;

                        case 3:
                            intent.setClass(getActivity(), CategoryActivity.class);
                            intent.putExtra("method", "hotauthor");
                            startActivity(intent);
                            break;

                        case 4:
                            //startActivity(new Intent(getActivity(),FindFragment.class));
                            break;

                    }
                }
            });


            return view;
        }

    }

    /**
     * 分类结果
     */
    class MyCategoryAdapter extends BaseAdapter
    {

        @Override
        public int getCount()
        {
            //return categoryNames.length;
            return listCategory.size();
        }

        @Override
        public Object getItem(int position)
        {
            return listCategory.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View view = View.inflate(getActivity(), R.layout.list_item_category, null);
            TextView textName = (TextView) view.findViewById(R.id.textName);
            textName.setText(listCategory.get(position).getName());

            textName.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Intent intent = new Intent();
                    intent.setClass(getActivity(), CategoryActivity.class);
                    intent.putExtra("categoryId", listCategory.get(position).getId() + "");
                    intent.putExtra("method", "category");

                    startActivity(intent);
                }
            });


            return view;
        }
    }


    /**
     * 热门书籍
     */
    class MyHotAdapter extends BaseAdapter
    {

        @Override
        public int getCount()
        {
            return listBooks.size();
        }

        @Override
        public Object getItem(int position)
        {
            return listBooks.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)

        {
            View view = View.inflate(getActivity(), R.layout.list_item_hot, null);

            TextView textName = (TextView) view.findViewById(R.id.textName);
            SmartImageView image = (SmartImageView) view.findViewById(R.id.ivIcon);

            textName.setText(listBooks.get(position).getBookName());

            image.setImageUrl(listBooks.get(position).getImagesPath());

            image.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Intent intent = new Intent(getActivity(), BookActivity.class);

                    intent.putExtra(AppConsrants.BOOK_ID, listBooks.get(position).getId() + "");
                    startActivity(intent);

                }
            });

            return view;
        }
    }


    /**
     * 热门作者
     */
    class HotAuthorAdapter extends BaseAdapter
    {
        @Override
        public int getCount()
        {
            return listAuthor.size();
        }

        @Override
        public Object getItem(int position)
        {
            return listAuthor.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View view = View.inflate(getActivity(), R.layout.list_item_hot, null);

            TextView textName = (TextView) view.findViewById(R.id.textName);
            SmartImageView image = (SmartImageView) view.findViewById(R.id.ivIcon);

            Author author = listAuthor.get(position);
            textName.setText(author.getName());
            image.setImageUrl(author.getSrc());

            return view;
        }
    }

    /**
     * 才你喜欢
     */
    class GuessAdapter extends BaseAdapter
    {

        @Override
        public int getCount()
        {
            return listSimple.size();
        }

        @Override
        public Object getItem(int position)
        {
            return listSimple.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            View view;
            if (convertView == null)
            {
                view = View.inflate(getActivity(), R.layout.list_item_main_guess, null);
            } else
            {
                view = convertView;
            }

            BookImageAddress b = listSimple.get(position);

            TextView textName = (TextView) view.findViewById(R.id.textName);
            SmartImageView image = (SmartImageView) view.findViewById(R.id.ivIcon);
            TextView textAddress = (TextView) view.findViewById(R.id.textAddress);

            textName.setText(b.getBookName().length()>=6?b.getBookName().substring(0,6)+"...":b.getBookName());
            image.setImageUrl(b.getImageSrc());
            textAddress.setText(b.getAddress());

            image.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Intent intent = new Intent(getActivity(), BookActivity.class);

                    intent.putExtra(AppConsrants.BOOK_ID,listSimple.get(position).getId()+"");
                    startActivity(intent);
                }
            });

            return view;
        }
    }
}
